<?php
class ArticleController extends  IndexController
{
    public function init()
    {
        parent::init();
        $this->utl              = Yaf_Registry::get("utl");
        $this->Session          = Yaf_Registry::get("session");
        $this->ArticleModel     = new ArticleModel();
        $this->CategoryModel    = new CategoryModel();
        $this->WebsiteModel     = new WebsiteModel();
        $this->CategoryTwoModel = new CategoryTwoModel();
        $this->UserLogsModel    = new LogsModel();
    }

    public function listAction()
    {
        $typicle_info       = $this->ArticleModel->articleType();
        $offset             = 10;
        $page               = $this->getRequest()->getParam("page",1);
        $startLimit         = $offset*($page-1);
        if ($this->getRequest()->isPost()) {
            $post               = $this->getRequest()->getPost();
            if($post['typical_id'] == 0 && $post['channel_id'] == 0){
                $article_info       = $this->ArticleModel->selectArticle($startLimit,$offset);
                $allCounts          = $this->ArticleModel->getAllCount();
                $allpage            = $this->utl::page('/Article/list', $page, $allCounts[0]['rows'], $offset, $status='');
            }
            else{   
                $article_info       = $this->ArticleModel->selectChooceArticle($startLimit,$offset,$post);
                $allCounts          = $this->ArticleModel->getChooceCount($post);
                $allpage            = $this->utl::page('/Article/list', $page, $allCounts[0]['rows'], $offset, $status='');
            }
            
        }
        else{
            $article_info       = $this->ArticleModel->selectArticle($startLimit,$offset);
            $allCounts          = $this->ArticleModel->getAllCount();
            $allpage            = $this->utl::page('/Article/list', $page, $allCounts[0]['rows'], $offset, $status='');
        }
        $channel_info       = $this->ArticleModel->selectChannel();
        $channel_list       = array();
        $article_list       = array();
        foreach ($channel_info as $key => $value) {
            $channel_list[$key] = $value;
            foreach ($typicle_info  AS $ks => $vs)
            {
                if ($value['article_type_id'] === $vs['article_type_id'])
                {
                    $channel_list[$key]['type_name'] = $vs['name'];break;
                }
            }
        }
        foreach ($article_info AS $k => $v)
        {
            $article_list[$k] = $v;
            $article_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $article_list[$k]['push_time']    = date('Y-m-d H:i:s', (int) $v['push_time']);
            $article_list[$k]['rows']         = $allCounts[0]['rows'];
            $article_list[$k]['every_page']   = $offset;
            $article_list[$k]['page']         = $allpage;
            foreach ($typicle_info  AS $ks => $vs)
            {
                if ($v['article_type_id'] === $vs['article_type_id'])
                {
                    $article_list[$k]['name'] = $vs['name'];break;
                }
            }
        }
        foreach ($article_list as $key => $value) 
        {
            if($value['status'] == 1)
            {
                $article_list[$key]['article_status'] = '推送失败';
            }
            else if($value['status'] == 0)
            {
                $article_list[$key]['article_status'] ='未推送';
            }
            else if($value['status'] == 2)
            {
                $article_list[$key]['article_status'] ='已推送';
            }
            if ($value['thumb'] == "") 
            {
                $article_list[$key]['pic']="";
            }
            else 
            {
                $article_list[$key]['pic'] = '<img src="/public/image/small_img.png"/>';;
            }

            $arr     = array('[img]','[br][br][br][br]','[&emsp][&emsp][&emsp][&emsp]','[&amp]','[p][&emsp][&emsp][/p]','[p][img][text-align:center]','[div][img][text-align:center]','[&nbsp]','[&emsp]','[img]','[/img]','[p][text-align:center]','[&ldquo]','[&rdquo]','[pstyle','[&lt]','[&gt]','[tablealign="center"]');
            $arr2    = array('[p][img]','','[&emsp][&emsp]','&','','[p style="text-align:center;"] [img src="','[div style="text-align:center;"] [img src="','&nbsp','&emsp;','[p style="text-align:center;"] [img src="','"] [/p]','[p style="text-align:center;"]','"','"','[p style','<','>','[table align="center"]');

            $content = str_replace($arr, $arr2, $value['content']);
             // $arr3 = array('[', ']');
             // $arr4 = array('<', '>');
             // $aa = str_replace($arr3, $arr4, $content);
           // var_dump($aa);
            $article_list[$key]['article_content'] = $content;
            $article_list[$key]['page'] = $allpage;
        }
        //var_dump($article_list);
        if(!empty($post)){
            exit(json_encode($article_list, true));
        }
        else{
            $this->getView()->assign("typicle_info",$typicle_info);
            $this->getView()->assign("channel_list",$channel_list);
            $this->getView()->assign("article_list",$article_list);
            $this->getView()->assign("allpage",$allpage);

        }

    }

    public function pageAction()
    {
        $typicle_info       = $this->ArticleModel->articleType();
        $offset             = 10;
        $page               = $this->getRequest()->getParam("page",1);
        $startLimit         = $offset*($page-1);
        $post = $this->getRequest()->getPost();
        if ($post['typical_id'] != 0  ) {
            $article_info      = $this->ArticleModel->selectChooceArticle($startLimit,$offset,$post);
            $allCounts         = $this->ArticleModel->getChooceCount($post);
            $allpage           = $this->utl::page('/Article/list', $page, $allCounts[0]['rows'], $offset, $status='');
        }
        else{
            if($post['status'] != ""  || $post['channel_id'] != 0){
                $article_info  = $this->ArticleModel->selectChooceArticle($startLimit,$offset,$post);
                $allCounts     = $this->ArticleModel->getChooceCount($post);
                $allpage       = $this->utl::page('/Article/list', $page, $allCounts[0]['rows'], $offset, $status='');
            }
            else{
                $article_info  = $this->ArticleModel->selectArticle($startLimit,$offset);
                $allCounts     = $this->ArticleModel->getAllCount();
                $allpage       = $this->utl::page('/Article/list', $page, $allCounts[0]['rows'], $offset, $status='');
            }
            
        }
        $article_list       = array();
        foreach ($article_info AS $k => $v)
        {
            $article_list[$k] = $v;
            $article_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $article_list[$k]['push_time']    = date('Y-m-d H:i:s', (int) $v['push_time']);
            $article_list[$k]['rows']         = $allCounts[0]['rows'];
            $article_list[$k]['every_page']   = $offset;
            foreach ($typicle_info  AS $ks => $vs)
            {
                if ($v['article_type_id'] === $vs['article_type_id'])
                {
                    $article_list[$k]['name'] = $vs['name'];break;
                }
            }
        }
        foreach ($article_list as $key => $value) 
        {
            if($value['status'] == 1)
            {
                $article_list[$key]['article_status'] = '推送失败';
            }
            else if($value['status'] == 0)
            {
                $article_list[$key]['article_status'] ='未推送';
            }
            else if($value['status'] == 2)
            {
                $article_list[$key]['article_status'] ='已推送';
            }
            if ($value['thumb'] == "") 
            {
                $article_list[$key]['pic']="";
            }
            else 
            {
                $article_list[$key]['pic'] = '<img src="/public/image/small_img.png"/>';;
            }

            $arr     = array('[img]','[br][br][br][br]','[&emsp][&emsp][&emsp][&emsp]','[&amp]','[p][&emsp][&emsp][/p]','[p][img][text-align:center]','[div][img][text-align:center]','[&nbsp]','[&emsp]','[img]','[/img]','[p][text-align:center]','[&ldquo]','[&rdquo]','[pstyle','[&lt]','[&gt]','[tablealign="center"]');
            $arr2    = array('[p][img]','','[&emsp][&emsp]','&','','[p style="text-align:center;"] [img src="','[div style="text-align:center;"] [img src="','&nbsp','&emsp;','[p style="text-align:center;"] [img src="','"] [/p]','[p style="text-align:center;"]','"','"','[p style','<','>','[table align="center"]');

            $content = str_replace($arr, $arr2, $value['content']);
             // $arr3 = array('[', ']');
             // $arr4 = array('<', '>');
             // $aa = str_replace($arr3, $arr4, $content);
            //var_dump($aa);
            $article_list[$key]['article_content'] = $content; 
        }
        //var_dump($allCounts);
        $this->getView()->assign("article_list",$article_list);
        exit(json_encode($article_list, true));
    }

    public function currentPageAction()
    {
        $typicle_info       = $this->ArticleModel->articleType();
        $offset             = 10;
        $page               = $this->getRequest()->getParam("page",1);
        $startLimit         = $offset*($page-1);
        $post = $this->getRequest()->getPost();
        if ($post['typical_id'] != 0 ) {
            $article_info  = $this->ArticleModel->selectChooceArticle($startLimit,$offset,$post);
            $allCounts     = $this->ArticleModel->getChooceCount($post);
            $allpage       = $this->utl::page('/Article/list', $page, $allCounts[0]['rows'], $offset, $status='');
            //echo "12";
        }
        else{
             if($post['status'] != ""  || $post['channel_id'] != 0){
                $article_info  = $this->ArticleModel->selectChooceArticle($startLimit,$offset,$post);
                $allCounts     = $this->ArticleModel->getChooceCount($post);
                $allpage       = $this->utl::page('/Article/list', $page, $allCounts[0]['rows'], $offset, $status='');
            }
            else {
                $article_info  = $this->ArticleModel->selectArticle($startLimit,$offset);
                $allCounts     = $this->ArticleModel->getAllCount();
                $allpage       = $this->utl::page('/Article/list', $page, $allCounts[0]['rows'], $offset, $status='');
            }
            /*$article_info       = $this->ArticleModel->selectArticle($startLimit,$offset);
            $allCounts          = $this->ArticleModel->getAllCount();
            $allpage=$this->utl::page('/Article/list', $page, $allCounts[0]['rows'], $offset, $status='');*/
            
        }
       // var_dump($allCounts);
        exit($allpage);
    }

    public function chanelAction()
    {
        if ($this->getRequest()->isPost()) {

            header('Content-type:application/json;charset:utf8');
            $post = $this->getRequest()->getPost();
            $res  = $this->ArticleModel->selectChannelByRobot($post);
            if (false === $res)
            {
                exit('{"status":false,"info":"查找失败！"}');
                return;
            }
            $result  =  json_encode($res);
            exit($result);
            //var_dump($post);
            //$rs = $this->CategoryModel->getNamnById($post);
            /*$rs = $this->WebsiteModel->selectSites();
            */
        }
    }


    public function delAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('Article', 'list');
            return;
        }
        $rs = $this->ArticleModel->del($id);
        $session_del = array();
        $user_info = $this->Session->user_info;
        foreach ($user_info as $k => $v) 
        {
            $session_del[$k]['user_id'] = $v['user_id'];
            $session_del[$k]['table_name'] = "article";
            $session_del[$k]['action'] = "del";
        }
        $this->UserLogsModel->addUser($session_del);
        header('Content-type:application/json;charset:utf8');
        if (false === $rs)
        {
            exit('{"status":false,"info":"删除失败！"}');
            return;
        }
        exit('{"status":true,"info":"删除成功"}');
        return true;
    }

    public function pushTimeAction()
    {
       if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $session_push = array();
            //$a         = array();
            $user_info = $this->Session->user_info;
            $result    = array_keys($post);
            $res_info  = json_decode($result[0],true);
            $res       = $this->ArticleModel->push($res_info);
            foreach ($user_info as $k => $v) 
            {
                $session_push['user_id']       = $v['user_id'];
                $session_push['status']        = 2;
                $session_push['sites']         = $res_info['address'];
                $session_push['oneModel']      = $res_info['oneModel'];
                $session_push['article_id']    = $res_info['articleId'];
                $session_push['article_title'] = $res_info['articleTitle'];
                $session_push['time']          = $res_info['time'];
            }
            $this->ArticleModel->addPush($session_push);
            //$this->ArticleModel->status($res_info);
            if (false === $res) 
            {   
                exit('{"status":false,"info":"推送失败！"}');
                return;
            }
            exit('{"status":true,"info":"推送成功！"}');
            return true;
        }
    }

    public function pushStatusAction()
    {
       if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $getPushBackInfo =  array_unique(explode(",", $post['article_info']));
            array_pop($getPushBackInfo);
            $article_info = array();
            foreach ($getPushBackInfo as $key => $value) {
                if($value == "Yes" || $value == "No"){
                    $article_info['status'] = $value;
                }
                else{
                    $article_info[$key]['article_id'] = $value;
                }
            }
            $res_article = $this->ArticleModel->status($article_info);
            $res_push    = $this->ArticleModel->updatePush($article_info);
            /*$session_push = array();
            $user_info    = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_push['user_id'] = $v['user_id'];
                $session_push['status'] = $post['status'];
                $session_push['sites'] = $res['address'];
                $session_push['oneModel'] = $res['oneModel'];
                $session_push['article_id'] = $res['articleId'];
                $session_push['time'] = $res['time'];
            }*/
            //$this->ArticleModel->updatePush($post);
            
            if (false === $res_article) 
            {   
                exit('{"status":false,"info":"推送失败！"}');
                return;
            }
            // else if($post['status'] == 2){
            //     exit('{"status":true,"info":"推送成功！"}');
            //    // return true;
            // }
            exit('{"status":true,"info":"推送成功！"}');
        }
    }

    public function editAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->redirect('Article', 'list');
            return;
        }
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->ArticleModel->edit($post, $id);
            $session_edit = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_edit[$k]['user_id'] = $v['user_id'];
                $session_edit[$k]['table_name'] = "article";
                $session_edit[$k]['action'] = "edit";
            }
            $this->UserLogsModel->addUser($session_edit);
            if (false === $rs)
            {
                exit('{"status":false,"info":"编辑失败！"}');
                return;
            }
            exit('{"status":true,"info":"编辑成功！"}');
        }
        $rs = $this->ArticleModel->getArticleById($id);
        $this->getView()->assign('articleInfo', $rs);
        $all=$this->ArticleModel->getTypicalName();
        $this->getView()->assign('typicalName', $all);
    }

    public function addressAction()
    {
        if ($this->getRequest()->isPost()) {

            header('Content-type:application/json;charset:utf8');
            //$rs = $this->CategoryModel->getNamnById($post);
            $rs = $this->WebsiteModel->selectSites();
            if (false === $rs)
            {
                exit('{"status":false,"info":"一级查找失败！"}');
                return;
            }
            $result  =  json_encode($rs);
            exit($result);
        }
    }

    public function moduleAction()
    {
        if ($this->getRequest()->isPost()) 
        {
            $post = $this->getRequest()->getPost();
            foreach ($post as $key => $value) {}
            //var_dump($value);
            header('Content-type:application/json;charset:utf8');
            if($value == 1)
            {
                $category           = array();
                $category_list      = array();
                $category_array     = array();
                $sites_info         = $this->WebsiteModel->selectSites();
                $category_info      = $this->CategoryModel->getCategoryInfo();
                foreach ($category_info AS $k => $v) 
                {
                    $category_list[$k]['catid']        = $v['catid'];
                    $category_list[$k]['parentid']     = $v['parentid'];
                    $category_list[$k]['arrparentid']  = $v['arrparentid'];
                    $category_list[$k]['catname']      = $v['catname'];
                    $category_list[$k]['arrchildid']   = $v['arrchildid'];
                }
                foreach ($category_list as $k => $v) 
                {
                $child = explode(",",$v['arrchildid']);
                $one       = explode(",",$v['arrparentid']);
                $one_count =count($one);
                    foreach ($child as $ks) 
                    {
                        if($v['catid'] == $ks && $one_count  == 1)
                        {
                            $category[$k]['catname'] = $v['catname'].",".$v['catid'];
                        }
                        else if($v['catid'] != $ks)
                        {
                            $catname=$this->CategoryModel->getCategoryChildInfo($ks);
                            $two_child = explode(",",$catname[0]['arrchildid']);
                            $two       = explode(",",$catname[0]['arrparentid']);
                            $two_count =count($two);
                            foreach ($two_child as $kk) 
                            {
                                if($catname[0]['catid'] == $kk && $two_count == 2)
                                {
                                    $category[$ks]['one'] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$catname[0]['catname'].",".$catname[0]['catid'];
                                }
                                else if($catname[0]['catid'] != $kk )
                                {
                                    $catname_twochild=$this->CategoryModel->getCategoryChildInfo($kk);
                                    $three_child = explode(",",$catname_twochild[0]['arrchildid']);
                                    $three      = explode(",",$catname_twochild[0]['arrparentid']);
                                    $three_count =count($three);
                                    foreach ($three_child as $kkk) 
                                    {
                                         if($catname_twochild[0]['catid'] == $kkk && $three_count == 3)
                                        {

                                            $category[$kk]['two'] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'│  '."&nbsp;&nbsp;".'├─  '.$catname_twochild[0]['catname'].",".$catname_twochild[0]['catid'];
                                        } 
                                    }        
                                }
                            } 
                        }    
                    }
                }  
            }
            else if($value == 2)
            {
                $list_info        = $this->WebsiteModel->selectZhss(); 
               // $rs = $this->CategoryModel->getNamesById();
                foreach ($list_info as $key => $value) {
                $childid = explode(",",$value['arrchildid']);
                $childLength = count ($childid);
                    if($value['parentid'] == 0){
                        $category[$value['catid']]['catname'] = $value['catname'].",".$value['catid'];

                        if($childLength>1){
                            foreach ($childid as $kc => $vc) {
                                $rs = $this->WebsiteModel->getZhssInfoByParentid($vc);
                                //var_dump($rs);
                                if($kc!=0 ){
                                    foreach ($rs as $ks => $vs) {
                                        $catid = explode(",",$vs['arrparentid']);
                                        $catLength = count ($catid);
                                        if($catLength == 2){
                                            //var_dump($rs[0]['catname'].",".$rs[0]['catid']);
                                            $category[$vc]['one'] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$rs[0]['catname'].",".$rs[0]['catid'];
                                      
                                        }
                                        $child_id = explode(",",$vs['arrchildid']);
                                        $childLen = count ($child_id);
                                        if($childLen>1){
                                            foreach ($child_id as $kd => $vd) {
                                                $res = $this->WebsiteModel->getZhssInfoByParentid($vd);
                                                if($kd!=0){
                                                    foreach ($res as $kes => $ves) {
                                                        $childrenId = explode(",",$ves['arrparentid']);
                                                        $childrenIdLength = count ($childrenId);
                                                        if($childrenIdLength == 3){
                                                            $category[$vd]['two'] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'│  '."&nbsp;&nbsp;".'├─  '.$res[0]['catname'].",".$res[0]['catid'];
                                                        }
                                                        $child_four = explode(",",$ves['arrchildid']);
                                                        $childLenFour = count ($child_four);
                                                        if($childLenFour>1){
                                                            foreach ($child_four as $kh => $vh) {
                                                                $result = $this->WebsiteModel->getZhssInfoByParentid($vh);
                                                                if($kh!=0){
                                                                    foreach ($result as $ket => $vet) {
                                                                        $childrenIdFour = explode(",",$vet['arrparentid']);
                                                                        $childrenIdLengthFour = count ($childrenIdFour);
                                                                        if($childrenIdLengthFour == 4){
                                                                            $category[$vh]['three'] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'└─'.$result[0]['catname'].",".$result[0]['catid'];
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } 
                            }
                        }
                    }
                }
            }
            
            foreach ($category as $key => $values) 
            {
                foreach ($values as $k1) 
                {
                    $category_array[$key] = $k1;
                }
            }
            $category_lists =array();
            $category_lists=array_values($category_array);
            $result  =  json_encode($category_lists);
            if (false === $category)
            {
                exit('{"status":false,"info":"二级查找失败！"}');
                return;
            }
            exit($result);
        }
    }

    public function twoAction()
    {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
            foreach ($post as $key => $value) {}
            header('Content-type:application/json;charset:utf8');
            $rs = $this->CategoryTwoModel->getCategoryTwoNameById($value);
            $result  =  json_encode($rs);
            if (false === $rs)
            {
                exit('{"status":false,"info":"三级查找失败！"}');
                return;
            }
            exit($result);
        }
    }

    public function searchAction()
    {
        if ($this->getRequest()->isPost()) {
            $post   = $this->getRequest()->getPost();
            $i      = 0;
            $result = array();
            header('Content-type:application/json;charset:utf8');
            $res    = $this->WebsiteModel->selectSearchCategory($post);
            if(count($res) == 1){
                if($res[0]['parentid'] != 0 && $res[0]['child'] == 0){
                    $parentid     = explode(",", $res[0]['arrparentid']);
                    $res_parent   = $this->WebsiteModel->selectSearchCategoryParent($parentid);
                    if(count($res_parent)<2){
                        $result[$i++] = $res_parent[0]['catname'].",".$res_parent[0]['catid'];
                        $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$res[0]['catname'].",".$res[0]['catid'];
                    }
                    else{
                        $result[$i++] = array_slice($res_parent,-2)[0]['catname'].",".array_slice($res_parent,-2)[0]['catid'];
                        $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".array_slice($res_parent,-1)[0]['catname'].",".array_slice($res_parent,-1)[0]['catid'];
                        $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'│  '."&nbsp;&nbsp;&nbsp;".'├─  '.$res[0]['catname'].",".$res[0]['catid']; 
                    }
                    
                }
                else if($res[0]['parentid'] == 0){
                    $childid      = explode(",", $res[0]['arrchildid']);
                    $res_child    = $this->WebsiteModel->selectSearchCategoryChild($childid);
                    $result[$i++] = $res[0]['catname'].",".$res[0]['catid'];
                    foreach ($res_child as $key => $value) {
                        if (count($value) == count($value, 1)) {
                            $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$value['catname'].",".$value['catid'];
                        } else {
                            foreach ($value as $k => $v) {
                                $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'│  '."&nbsp;&nbsp;&nbsp;".'├─  '.$v['catname'].",".$v['catid'];
                            }  
                        }
                    }
                    
                }
                else if($res[0]['parentid'] != 0 && $res[0]['child'] ==1 ){
                    $parentid = explode(",", $res[0]['arrparentid']);
                    $childid  = explode(",", $res[0]['arrchildid']);
                    
                    $res_parChild = $this->WebsiteModel->selectSearchCategoryParent($parentid);
                    if(count($res_parChild) == 1){
                        $result[$i++] = $res_parChild[0]['catname'].",".$res_parChild[0]['catid'];
                    //$result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$res_parChild[1]['catname'].",".$res_parChild[1]['catid'];
                    $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$res[0]['catname'].",".$res[0]['catid'];
                    }
                    else{

                       $result[$i++] = $res_parChild[1]['catname'].",".$res_parChild[1]['catid'];
                    $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$res_parChild[1]['catname'].",".$res_parChild[1]['catid'];
                    $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$res[0]['catname'].",".$res[0]['catid']; 
                    }
                    
                    $res_parChild = $this->WebsiteModel->selectSearchCategoryParChild($childid,$res[0]['catid']);
                    foreach ($res_parChild as $key => $value) {
                        $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'│  '."&nbsp;&nbsp;&nbsp;".'├─  '.$value['catname'].",".$value['catid'];
                    }
                }
            }
            
            else{
                foreach ($res as $key => $value) {
                    if($value['parentid'] == 0){
                        $childid      = explode(",", $value['arrchildid']);
                        $res_child    = $this->WebsiteModel->selectSearchCategoryChild($childid);
                        $result[$i++] = $res[0]['catname'].",".$res[0]['catid'];
                        foreach ($res_child as $key => $value) {
                            if (count($value) == count($value, 1)) {
                                $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$value['catname'].",".$value['catid'];
                            } else {
                                foreach ($value as $k => $v) {
                                    $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'│  '."&nbsp;&nbsp;&nbsp;".'├─  '.$v['catname'].",".$v['catid'];
                                }  
                            }
                        }
                    }
                    else if($value['parentid'] != 0 && $value['child'] == 0){
                        $parentid   = explode(",", $value['arrparentid']);
                        $res_parent = $this->WebsiteModel->selectSearchCategoryParent($parentid);
                        //ar_dump(array_slice($res_parent,-1)[0]['catname'].",".array_slice($res_parent,-1)[0]['catid']);
                        if(count($res_parent) >= 2){
                            $result[$i++] = array_slice($res_parent,-2)[0]['catname'].",".array_slice($res_parent,-2)[0]['catid'];
                            $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".array_slice($res_parent,-1)[0]['catname'].",".array_slice($res_parent,-1)[0]['catid'];
                            $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'│  '."&nbsp;&nbsp;&nbsp;".'├─  '.$res[0]['catname'].",".$res[0]['catid'];
                        }
                        else{
                            $result[$i++] = $res_parent[0]['catname'].",".$res_parent[0]['catid'];
                            $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$res[0]['catname'].",".$res[0]['catid'];
                        }
                    }

                    else if($value['parentid'] != 0 && $value['child'] ==1 ){
                        $parentid = explode(",", $value['arrparentid']);
                        $childid  = explode(",", $value['arrchildid']);
                        
                        $res_parChild = $this->WebsiteModel->selectSearchCategoryParent($parentid);
                        if(count($res_parChild) < 2){
                            $result[$i++] = $res_parChild[0]['catname'].",".$res_parChild[0]['catid'];
                            $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$res[0]['catname'].",".$res[0]['catid'];
                        }
                        else{
                            //var_dump($res_parChild[1]['catname']);
                            $result[$i++] = $res_parChild[1]['catname'].",".$res_parChild[1]['catid'];
                            //$result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$res_parChild[1]['catname'].",".$res_parChild[1]['catid'];
                            $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'├─  '."&nbsp;&nbsp;".$res[0]['catname'].",".$res[0]['catid'];
                        }
                        $res_parChild = $this->WebsiteModel->selectSearchCategoryParChild($childid,$value['catid']);
                        foreach ($res_parChild as $k => $v) {
                            //if($v['child'] == 0){
                                $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'│  '."&nbsp;&nbsp;&nbsp;".'├─  '.$v['catname'].",".$v['catid'];
                            //}
                            //else{
                                $childid = explode(",", $v['arrchildid']);
                                $res_parChildren = $this->WebsiteModel->selectSearchCategoryParChild($childid,$v['catid']);
                                foreach ($res_parChildren as $ks => $vs) {
                                    $result[$i++] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".'└─'.$vs['catname'].",".$vs['catid'];
                                }
                           // }
                        }
                            
                    }

                }
            }
            
            //if($res[0]['child'] == 0){
                $result  =  json_encode($result);
            //}
            exit($result);
            
        }
    }

  

    
}
?>